import { useEffect, useState } from 'react'
import { HeaderItem, ProductItem, fetchProductRemAPI } from '@/apis/home'
// import { log } from 'console'

function useHeader() {
  const [isLoading, setLoading] = useState<boolean>(false)
  // 标题
  const [headers, setHeaders] = useState<HeaderItem["caption"]>()
  // 图片
  const [imgs, setImgs] = useState<ProductItem[]>([])

  useEffect(() => {
    const getHeader = async () => {
      try {
        setLoading(true)
        const res = await fetchProductRemAPI()
        // console.log(res);
        setHeaders(res.data.head[0].caption)
        setImgs(res.data.data)
        setLoading(false)
      } catch (error) {
        throw new Error('fetch setHeaders error')
      }
    }
    getHeader()
  }, [])
  // 暴露数据
  return {
    headers,imgs,isLoading
  }
}

export { useHeader }
